#include <MyApp.h>

MyApp::MyApp()
{
  _sceneManager = NULL;
  _framelistener = NULL;
}

MyApp::~MyApp()
{
  delete _root;
  delete _framelistener;
}

int MyApp::start()
{
  _root = new Ogre::Root();
  
  if(!_root->restoreConfig())
  {
    _root->showConfigDialog();
    _root->saveConfig();
  }
  
  Ogre::RenderWindow* window = _root->initialise(true, "Don Simon");
  _sceneManager = _root->createSceneManager(Ogre::ST_GENERIC);
  
  Ogre::Camera* cam = _sceneManager->createCamera("MainCamera");
  cam->setPosition(Ogre::Vector3(0,5,5));
  cam->lookAt(Ogre::Vector3(0,0,0));
  cam->setNearClipDistance(0.1);
  cam->setFarClipDistance(100);
  
  Ogre::Viewport* viewport = window->addViewport(cam);
  viewport->setBackgroundColour(ColourValue::White);
  double width = viewport->getActualWidth();
  double height = viewport->getActualHeight();
  cam->setAspectRatio(width / height);
  
  loadResources();
  createScene();
  createOverlay();
  
  _framelistener = new MyFrameListener(window, cam, _overlayManager, _sceneManager);
  _root->addFrameListener(_framelistener);
  
  _root->startRendering();
  return 0;
}

void MyApp::loadResources()
{
  Ogre::ConfigFile cf;
  cf.load("resources.cfg");
  
  Ogre::ConfigFile::SectionIterator sI = cf.getSectionIterator();
  Ogre::String sectionstr, typestr, datastr;

  while (sI.hasMoreElements())
  {
    sectionstr = sI.peekNextKey();
    Ogre::ConfigFile::SettingsMultiMap *settings = sI.getNext();
    Ogre::ConfigFile::SettingsMultiMap::iterator i;

    for (i = settings->begin(); i != settings->end(); ++i)
    {
      typestr = i->first;
      datastr = i->second;
      Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
          datastr, typestr, sectionstr);        
    }
  }

  Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

void MyApp::createScene()
{
  // Nodo Simon
  SceneNode *simon = _sceneManager->createSceneNode("Simon");
  Entity *entSimon = _sceneManager->createEntity("MeshSimon.mesh");
  entSimon->setQueryFlags(STAGE);
  simon->attachObject(entSimon);
  _sceneManager->getRootSceneNode()->addChild(simon);
  simon->setPosition(Vector3(0, 0, 0));

  SceneNode *rojo = _sceneManager->createSceneNode("Rojo");
  Entity *entRojo = _sceneManager->createEntity("MeshRojo.mesh");
  entRojo->setQueryFlags(BOTON);
  rojo->attachObject(entRojo);
  simon->addChild(rojo);
  rojo->setPosition(Vector3(0, 0, 0));

  SceneNode *verde = _sceneManager->createSceneNode("Verde");
  Entity *entVerde = _sceneManager->createEntity("MeshVerde.mesh");
  entVerde->setQueryFlags(BOTON);
  verde->attachObject(entVerde);
  simon->addChild(verde);
  verde->setPosition(Vector3(0, 0, 0));

  SceneNode *azul = _sceneManager->createSceneNode("Azul");
  Entity *entAzul = _sceneManager->createEntity("MeshAzul.mesh");
  entAzul->setQueryFlags(BOTON);
  azul->attachObject(entAzul);
  simon->addChild(azul);
  azul->setPosition(Vector3(0, 0, 0));

  SceneNode *amarillo = _sceneManager->createSceneNode("Amarillo");
  Entity *entAmarillo = _sceneManager->createEntity("MeshAmarillo.mesh");
  entAmarillo->setQueryFlags(BOTON);
  amarillo->attachObject(entAmarillo);
  simon->addChild(amarillo);
  amarillo->setPosition(Vector3(0, 0, 0)); 

  // Plano del suelo
  Plane groundPlane(Vector3::UNIT_Y, 0);
  MeshManager::getSingleton().createPlane("groundPlane",
	ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, groundPlane,
	80,80,1,1,true,1,20,20,Vector3::UNIT_Z);

  SceneNode* groundNode = _sceneManager->createSceneNode("groundNode");
  Entity* groundEnt = _sceneManager->createEntity("planeEnt", "groundPlane");
  groundEnt->setMaterialName("Ground");

  groundNode->attachObject(groundEnt);

  // Luces
  _sceneManager->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_MODULATIVE);
  Ogre::Light* light = _sceneManager->createLight("light");
  light->setType(Ogre::Light::LT_POINT);
  light->setPosition(Ogre::Vector3(-1,5,1));
  groundNode->attachObject(light);

  _sceneManager->getRootSceneNode()->addChild(groundNode);
}

void MyApp::createOverlay()
{
  _overlayManager = Ogre::OverlayManager::getSingletonPtr();
  _overlayManager->getByName("Cursor")->show();
  _overlayManager->getByName("GUI")->show();
  
  _overlayManager->getOverlayElement("menuImage")->show();
  _overlayManager->getOverlayElement("menuPlayImage")->hide();
  _overlayManager->getOverlayElement("menuExtrasImage")->hide();
  _overlayManager->getOverlayElement("menuRecordsImage")->hide();
  _overlayManager->getOverlayElement("menuQuitImage")->hide();
  _overlayManager->getOverlayElement("Records")->hide();
  _overlayManager->getOverlayElement("Extras")->hide();
  _overlayManager->getOverlayElement("secuencia")->hide();
  _overlayManager->getOverlayElement("jugar")->hide();
}
